#!/bin/bash
set -ex
set -o pipefail

if [ -z "$1" ]
then
		BRANCH='master'
else
		BRANCH=$1
fi

commit_id=`docker exec -it  llvm-build /bin/bash -c "cd /srv/workspace/llvm-release/${BRANCH}/toolchain/llvm-project && git rev-parse --short HEAD"`
commit_id=$(echo $commit_id | sed 's/\r//g')
commit_id=$(echo ${commit_id:0:6})
date="20250320"
WORK_DIR=$(cd "$(dirname $0)";pwd)

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_arm64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_darwin-arm64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_arm64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx-ndk_darwin-arm64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_arm64

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_x86_64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_darwin-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_x86_64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx-ndk_darwin-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/darwin_x86_64

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_linux-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx-ndk_linux-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux_aarch64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_linux_aarch64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux_aarch64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx-ndk_linux-aarch64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/linux_aarch64

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/ohos_arm64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_ohos-arm64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/ohos_arm64
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx_ndk_ohos-arm64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/ohos_arm64

mkdir -p ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/windows
cp ${WORK_DIR}/${BRANCH}/packages/target_location/clang_windows-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/windows
cp ${WORK_DIR}/${BRANCH}/packages/target_location/libcxx-ndk_windows-x86_64* ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}/windows

cp ${WORK_DIR}/${BRANCH}/packages/clang_darwin-arm64-${commit_id}-${date}/manifest.xml ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}

cd ${WORK_DIR}/${BRANCH} && tar -zcvf  15.0.4-${commit_id}.tar.gz   ${WORK_DIR}/${BRANCH}/15.0.4-${commit_id}
cd -

